package eu.drseus.qiwidrone.prototype;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import eu.drseus.qiwidrone.prototype.horizon.ArtificialHorizon;
import eu.drseus.qiwidrone.prototype.joystick.Joystick;

public class DroneControlView extends View implements SensorEventListener{

	private Context context;
	private Display mDisplay;
	
	private Joystick joystick;
	private ArtificialHorizon horizon;
	
	private Sensor mAccelerometer;
	private float mSensorXPercent;
	private float mSensorYPercent;
	
	public DroneControlView(Context context, Display mDisplay, Sensor mAccelerometer) {
		super(context);
		this.context = context;
		this.mDisplay = mDisplay;
		this.mAccelerometer = mAccelerometer;
		setBackgroundColor(Color.WHITE);
		
		joystick = new Joystick(context,this);
		horizon = new ArtificialHorizon(context, this);
	}
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		
		initJoystick();
		initHorizon();
	}
	
	private void initHorizon() {
		horizon.setOrigin(getWidth()/2-horizon.getWidth(), getHeight()/2-horizon.getHeight()/2);
	}

	private void initJoystick() {
		joystick.setJoystickCoordinates(0, 0);
		joystick.setOrigin(getWidth()/2+joystick.getWidth()/4, getHeight()/2-joystick.getHeight()/2);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		//Draw Joystick
		joystick.draw(canvas);
		
		//Draw Horizon
		horizon.setRoll(mSensorXPercent);
		horizon.setPitch(mSensorYPercent);
		horizon.draw(canvas);
		
		canvas.save();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		joystick.onTouchEvent(event);
		invalidate();
		return true;//return super.onTouchEvent(event);
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {}

	@Override
	public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
        
        float mSensorX = 0;
        float mSensorY = 0;
        
        switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
                break;
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
                break;
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
                break;
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
                break;
        }

        mSensorX = mSensorX + 10f;
        mSensorY = mSensorY + 10f;
        
        mSensorXPercent = 100-(mSensorX * 5f);
        mSensorYPercent = mSensorY * 5f;
        
        invalidate();
	}
	
}
